package com.poxz.algorithm.template;

import java.util.*;

/**
 * Java常用数据结构
 *
 * @author poxz
 */
public class DataStructures {

    /**
     * ArrayList示例
     */
    static void arrayList() {
        List<Integer> list = new ArrayList<>();

        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println("添加后: " + list);

        System.out.println("第2个元素: " + list.get(1));

        list.set(1, 99);
        System.out.println("修改后: " + list);

        list.remove(0);
        System.out.println("删除后: " + list);

        System.out.println("大小: " + list.size());

        System.out.print("遍历: ");
        for (int x : list) {
            System.out.print(x + " ");
        }
        System.out.println("\n");
    }

    /**
     * HashMap示例
     */
    static void hashMap() {
        Map<String, Integer> map = new HashMap<>();

        map.put("apple", 5);
        map.put("banana", 3);
        map.put("orange", 7);
        System.out.println("添加后: " + map);

        System.out.println("apple的数量: " + map.get("apple"));
        System.out.println("包含banana? " + map.containsKey("banana"));
        System.out.println("grape的数量: " + map.getOrDefault("grape", 0));

        System.out.println("遍历:");
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            System.out.println("  " + e.getKey() + ": " + e.getValue());
        }
        System.out.println();
    }

    /**
     * HashSet示例
     */
    static void hashSet() {
        Set<Integer> set = new HashSet<>();

        set.add(1);
        set.add(2);
        set.add(2);
        set.add(3);
        System.out.println("添加后(自动去重): " + set);

        System.out.println("包含2? " + set.contains(2));

        set.remove(2);
        System.out.println("删除后: " + set);
        System.out.println();
    }

    /**
     * PriorityQueue - 优先队列（堆）示例
     */
    static void priorityQueue() {
        // 小根堆
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        minHeap.add(5);
        minHeap.add(2);
        minHeap.add(8);
        minHeap.add(1);
        System.out.print("小根堆弹出: ");
        while (!minHeap.isEmpty()) {
            System.out.print(minHeap.poll() + " ");
        }
        System.out.println();

        // 大根堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
        maxHeap.add(5);
        maxHeap.add(2);
        maxHeap.add(8);
        maxHeap.add(1);
        System.out.print("大根堆弹出: ");
        while (!maxHeap.isEmpty()) {
            System.out.print(maxHeap.poll() + " ");
        }
        System.out.println("\n");
    }

    /**
     * 栈 - 用Deque实现
     */
    static void stack() {
        Deque<Integer> stack = new ArrayDeque<>();

        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println("入栈后: " + stack);

        System.out.println("栈顶: " + stack.peek());

        System.out.print("出栈: ");
        while (!stack.isEmpty()) {
            System.out.print(stack.pop() + " ");
        }
        System.out.println("\n");
    }

    /**
     * 队列 - 用Deque实现
     */
    static void queue() {
        Deque<Integer> queue = new ArrayDeque<>();

        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println("入队后: " + queue);

        System.out.println("队首: " + queue.peek());

        System.out.print("出队: ");
        while (!queue.isEmpty()) {
            System.out.print(queue.poll() + " ");
        }
        System.out.println("\n");
    }

    /**
     * 双端队列示例
     */
    static void deque() {
        Deque<Integer> deque = new ArrayDeque<>();

        deque.offerFirst(1);
        deque.offerFirst(2);
        System.out.println("头部添加: " + deque);

        deque.offerLast(3);
        deque.offerLast(4);
        System.out.println("尾部添加: " + deque);

        System.out.println("头部弹出: " + deque.pollFirst());
        System.out.println("尾部弹出: " + deque.pollLast());
        System.out.println("最终: " + deque);
        System.out.println();
    }

    /**
     * TreeSet - 有序集合示例
     */
    static void treeSet() {
        TreeSet<Integer> set = new TreeSet<>();
        set.add(5);
        set.add(2);
        set.add(8);
        set.add(1);
        System.out.println("自动排序: " + set);

        System.out.println("最小值: " + set.first());
        System.out.println("最大值: " + set.last());
        System.out.println(">=3的最小值: " + set.ceiling(3));
        System.out.println("<=6的最大值: " + set.floor(6));
        System.out.println();
    }

    /**
     * TreeMap - 有序映射示例
     */
    static void treeMap() {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(3, "three");
        map.put(1, "one");
        map.put(2, "two");
        System.out.println("自动排序: " + map);

        System.out.println("第一个键: " + map.firstKey());
        System.out.println("最后一个键: " + map.lastKey());
        System.out.println();
    }

    /**
     * 功能测试
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        System.out.println("=== Java 数据结构使用示例 ===\n");

        arrayList();
        hashMap();
        hashSet();
        priorityQueue();
        stack();
        queue();
        deque();
        treeSet();
        treeMap();
    }
}
